﻿using System;
using System.Text;

namespace TrackingServerCore
{
    partial class Tracking
    {
        GPSTrackingDataClassesDataContext dt = new GPSTrackingDataClassesDataContext();

        public Tracking(int vehicleID, DateTime timeStamp,
            decimal longitude, decimal latitude, float altitude,
            short heading, string address, byte speed, int distanceKm,
            bool doorStatus, bool engineStatus, bool gpsStatus, bool gprsStatus, short currentFuel)
        {
            _vehicleID = vehicleID;
            _timeStamp = timeStamp;
            _longitude = longitude;
            _latitude = latitude;
            _altitude = altitude; //m unit
            _heading = heading;
            _address = address;
            _speed = speed; //kmph unit
            _distanceKm = distanceKm; //km unit
            _doorStatus = doorStatus; //close
            _engineStatus = engineStatus; //start
            _gpsStatus = gpsStatus; //ok
            _gprsStatus = gprsStatus; //ok
            _currentFuel = currentFuel; //lit
        }

        //public void Add(int vehicleID, DateTime timeStamp,
        //    decimal longitude, decimal latitude, float altitude,
        //    short heading, string address, byte speed, int distanceKm,
        //    bool doorStatus, bool engineStatus, bool gpsStatus, bool gprsStatus, short currentFuel)
        //{
        //    Tracking objTracking = new Tracking();
        //    objTracking.vehicleID = vehicleID;
        //    objTracking.timeStamp = timeStamp;
        //    objTracking.longitude = longitude;
        //    objTracking.latitude = latitude;
        //    objTracking.altitude = altitude; //m unit
        //    objTracking.heading = heading;
        //    objTracking.address = address;
        //    objTracking.speed = speed; //kmph unit
        //    objTracking.distanceKm = distanceKm; //km unit
        //    objTracking.doorStatus = doorStatus; //close
        //    objTracking.engineStatus = engineStatus; //start
        //    objTracking.gpsStatus = gpsStatus; //ok
        //    objTracking.gprsStatus = gprsStatus; //ok
        //    objTracking.currentFuel = currentFuel; //lit
        //}

        public void InsertMessage(Tracking objTracking)
        {
            Tracking localTracking = new Tracking();
            
            localTracking.vehicleID = objTracking.vehicleID;
            localTracking.timeStamp = objTracking.timeStamp;
            localTracking.longitude = objTracking.longitude;
            localTracking.latitude = objTracking.latitude;
            localTracking.altitude = objTracking.altitude;
            localTracking.heading = objTracking.heading;
            localTracking.address = objTracking.address;
            localTracking.speed = objTracking.speed;
            localTracking.distanceKm = objTracking.distanceKm;
            localTracking.doorStatus = objTracking.doorStatus;
            localTracking.engineStatus = objTracking.engineStatus;
            localTracking.gpsStatus = objTracking.gpsStatus;
            localTracking.gprsStatus = objTracking.gprsStatus;
            localTracking.currentFuel = objTracking.currentFuel;

            dt.Trackings.InsertOnSubmit(localTracking);
            dt.SubmitChanges();
        }

        public byte[] ConvertObjectToByteArray()
        {
            int len = 0;
            byte[] data = new byte[51]; //51 bytes for the object
            
            byte[] convert = BitConverter.GetBytes(_vehicleID); //4 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            convert = BitConverter.GetBytes(_timeStamp.Ticks); //8 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            convert = BitConverter.GetBytes((double)_longitude.Value); //8 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            convert = BitConverter.GetBytes((double)_latitude.Value); //8 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            convert = BitConverter.GetBytes(_altitude.Value); //4 bytes
            convert.CopyTo(data, len);
            len += convert.Length;
            
            convert = BitConverter.GetBytes(_heading.Value); //2 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            //convert = Encoding.ASCII.GetBytes(_address);
            //convert.CopyTo(data, len);
            //len += convert.Length;

            convert = new byte[]{ _speed.Value }; //1 byte
            convert.CopyTo(data, len);
            len += convert.Length;

            convert = BitConverter.GetBytes(_distanceKm.Value); //4 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            convert = BitConverter.GetBytes(_doorStatus.Value); //1 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            convert = BitConverter.GetBytes(_engineStatus.Value); //1 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            convert = BitConverter.GetBytes(_gpsStatus.Value); //1 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            convert = BitConverter.GetBytes(_gprsStatus.Value); //1 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            convert = BitConverter.GetBytes(_currentFuel.Value); //2 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            return data;
        }

        public void ConvertByteArrayToObject(byte[] data)
        {
            int len = 0;
            
            _vehicleID = BitConverter.ToInt32(data, 0); //4 bytes
            len += 4;

            _timeStamp = new DateTime(BitConverter.ToInt64(data, len)); //8 bytes
            len += 8;

            _longitude = (decimal)BitConverter.ToDouble(data, len); //8 bytes
            len += 8;

            _latitude = (decimal)BitConverter.ToDouble(data, len); //8 bytes
            len += 8;

            _altitude = BitConverter.ToSingle(data, len); //4 bytes
            len += 4;

            _heading = BitConverter.ToInt16(data, len); //2 bytes
            len += 2;

            _speed = data[len]; //1 bytes
            len++;

            _distanceKm = BitConverter.ToInt16(data, len); //4 bytes
            len += 4;

            _doorStatus = BitConverter.ToBoolean(data,len); //1 bytes
            len++;

            _engineStatus = BitConverter.ToBoolean(data,len); //1 bytes
            len++;

            _gpsStatus = BitConverter.ToBoolean(data,len); //1 bytes
            len++;
            
            _gprsStatus = BitConverter.ToBoolean(data,len); //1 bytes
            len++;
            
            _currentFuel = BitConverter.ToInt16(data,len); //4 bytes
            len += 4;
        }

        //public Tracking ConvertBytesToObject(byte[] packet)
        //{

        //}
    }
}
